package com.hfh.io.exer;

import org.junit.Test;
import sun.nio.cs.ext.GBK;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class InputStreamReaderDemo {
    @Test
    public void test1() throws IOException {
        try(InputStreamReader isr = new InputStreamReader(
                new FileInputStream("file\\a.txt"));
        OutputStreamWriter osw = new OutputStreamWriter(
                new FileOutputStream("file\\c.txt"),"GBK")){
            char[] arr = new char[1024];
            int len;
            while((len=isr.read(arr))!=-1){
//                System.out.println(new String(arr, 0, len));
                osw.write(arr, 0, len);
            }
            osw.flush();
        }
    }

    @Test
    public void test() throws IOException {
        try(InputStream is = new FileInputStream("file\\a.txt");
            InputStreamReader isr = new InputStreamReader(is);){
            //使用转换流的默认编码
            //与FileReader fr = new FileReader("file\\a.txt");等价
            char[] arr = new char[1024];
            int len;
            while((len=isr.read(arr))!=-1){
                System.out.println(new String(arr, 0, len));
            }
        }
    }
}
